package com.webex.ta.hydra.gui.menu;

import com.webex.ta.hydra.gui.MenuFactory;
import com.webex.ta.hydra.gui.action.ActionNames;
import com.webex.ta.hydra.gui.action.KeyStrokes;
import com.webex.ta.hydra.gui.action.LoadRecentProject;
import com.webex.ta.hydra.util.HydraUtils;

import javax.swing.*;
import java.util.*;
import java.util.List;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-27
 * Time: 9:27:06
 */
public class HydraMenuBar extends JMenuBar {
    private JMenu fileMenu, searchMenu, runMenu, macroMenu, helpMenu;

    private DynamicMenu editMenu, pluginsMenu;

    private JMenuItem file_new, file_open, file_close, file_save, file_save_as, file_exit;

    private List file_mru_files = new LinkedList();

    private JMenuItem search_find, search_replace;

    private JMenuItem run_run, run_debug, run_pause, run_resume, run_stop, run_clear, run_export;

    private JMenuItem macro_edit;

    private JMenuItem help_about;



    public HydraMenuBar() {
        super();
        populateMenus();
    }

    private void populateMenus() {
        makeFileMenu();
        makeEditMenu();
        makeSearchMenu();
        makeRunMenu();
        makeMacroMenu();
        makeOptionMenu();
        makeHelpMenu();


        add(fileMenu);
        add(editMenu);
        add(searchMenu);
        add(runMenu);
        add(macroMenu);
        add(pluginsMenu);
        add(helpMenu);
    }

    private void makeFileMenu() {
        fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');

        file_new = MenuFactory.makeMenuItem("New", 'N', "New", ActionNames.NEW, KeyStrokes.NEW);
        file_new.setIcon(HydraUtils.getImage("new.gif"));
        file_open = MenuFactory.makeMenuItem("Open", 'O', "Open", ActionNames.OPEN, KeyStrokes.OPEN);
        file_open.setIcon(HydraUtils.getImage("prj_obj.gif"));
        file_save = MenuFactory.makeMenuItem("Save", 'S', "Save", ActionNames.SAVE, KeyStrokes.SAVE);
        file_save.setIcon(HydraUtils.getImage("save.gif"));
        file_save_as = MenuFactory.makeMenuItem("Save As", 'A', "Save As", ActionNames.SAVE_AS, KeyStrokes.SAVE_AS);
        file_close = MenuFactory.makeMenuItem("Close", 'C', "Close", ActionNames.CLOSE, KeyStrokes.CLOSE);
        file_close.setIcon(HydraUtils.getImage("close.png"));
        file_exit = MenuFactory.makeMenuItem("Exit", 'X', "Exit", ActionNames.EXIT, KeyStrokes.EXIT);

        fileMenu.add(file_new);
        fileMenu.add(file_open);
        fileMenu.add(file_save);
        fileMenu.add(file_save_as);
        fileMenu.add(file_close);
        fileMenu.addSeparator();

        file_mru_files = LoadRecentProject.getRecentFileMenuItems();
        for (Object file_mru_file : file_mru_files) {
            fileMenu.add((JComponent) file_mru_file);
        }
        fileMenu.add(file_exit);
    }

    private void makeRunMenu() {
        runMenu = new JMenu("Run");
        runMenu.setMnemonic('U');

        run_run = MenuFactory.makeMenuItem("Run", 'R', "Run", ActionNames.RUN, KeyStrokes.RUN);
        run_run.setIcon(HydraUtils.getImage("run.gif"));
        run_debug = MenuFactory.makeMenuItem("Debug", 'D', "Debug", ActionNames.DEBUG);
        run_debug.setIcon(HydraUtils.getImage("debug_exc.gif"));
        run_pause = MenuFactory.makeMenuItem("Pause", 'P', "Pause", ActionNames.PAUSE);
        run_pause.setIcon(HydraUtils.getImage("pause.gif"));
        run_resume = MenuFactory.makeMenuItem("Resume", 'U', "Resume", ActionNames.RESUME);
        run_resume.setIcon(HydraUtils.getImage("resume_co.gif"));
        run_stop = MenuFactory.makeMenuItem("Stop", 'S', "Stop", ActionNames.STOP);
        run_stop.setIcon(HydraUtils.getImage("stop.gif"));
        run_clear = MenuFactory.makeMenuItem("Clear result", 'E', "Clear result", ActionNames.CLEAR, KeyStrokes.CLEAR);
        run_clear.setIcon(HydraUtils.getImage("clear.gif"));
        run_export = MenuFactory.makeMenuItem("Export result", 'O', "Export result", ActionNames.EXPORT_RESULT);
        run_export.setIcon(HydraUtils.getImage("export.gif"));

        runMenu.add(run_run);
        runMenu.add(run_debug);
        runMenu.add(run_pause);
        runMenu.add(run_resume);
        runMenu.add(run_stop);
        runMenu.addSeparator();
        runMenu.add(run_clear);
        runMenu.add(run_export);
    }

    private void makeMacroMenu() {
        macroMenu = new JMenu("Macro");
        macroMenu.setMnemonic('M');

        macro_edit = MenuFactory.makeMenuItem("Edit...", 'E', "Edit", ActionNames.EDIT_MACRO);
        macro_edit.setIcon(HydraUtils.getImage("macro.gif"));

        macroMenu.add(macro_edit);
    }

    private void makeOptionMenu() {
        pluginsMenu = new DynamicMenu("plugins", "Plugins");
        pluginsMenu.setMnemonic('P');
        pluginsMenu.setProvider(new PluginsProvider());

//        option_pluginmgr = MenuFactory.makeMenuItem("Plugin Manager...", 'E', "Plugin Manager...", ActionNames.PLUGIN_MGR);
//        option_pluginmgr.setIcon(HydraUtils.getImage("plugins.gif"));
    }

    private void makeEditMenu() {
        editMenu = new DynamicMenu("Edit", "Edit");
        editMenu.setMnemonic('E');
        editMenu.setProvider(new EditMenuProvider());



    }

    private void makeSearchMenu() {
        searchMenu = new JMenu("Search");
        searchMenu.setMnemonic('S');

        search_find = MenuFactory.makeMenuItem("Find", 'F', "Find", ActionNames.FIND, KeyStrokes.FIND, HydraUtils.getImage("find.png"));
        searchMenu.add(search_find);
    }

    private void makeHelpMenu() {
        helpMenu = new JMenu("Help");
        helpMenu.setMnemonic('H');

        help_about = MenuFactory.makeMenuItem("About Hydra", 'A', "About Hydra", ActionNames.ABOUT);

        helpMenu.add(help_about);
    }

    public void projectFileLoaded(String file) {
        if(file_mru_files != null && file != null) {
            LoadRecentProject.updateRecentFileMenuItems(file_mru_files, file);
        }
    }

    public void testStart() {
        run_run.setEnabled(false);
        run_debug.setEnabled(false);
        run_pause.setEnabled(true);
        run_resume.setEnabled(false);
        run_stop.setEnabled(true);
    }

    public void testStop() {
        run_run.setEnabled(true);
        run_debug.setEnabled(true);
        run_pause.setEnabled(false);
        run_resume.setEnabled(false);
        run_stop.setEnabled(false);
    }

    public void testPaused() {
        run_run.setEnabled(false);
        run_debug.setEnabled(false);
        run_pause.setEnabled(false);
        run_resume.setEnabled(true);
        run_stop.setEnabled(true);
    }

    public void updateEditMenu() {
        editMenu.update();
    }
}
